package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.UserQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private UserMapper userMapper;

    @Override
    /**
     * 检查用户电话号码是否已存在于系统中
     *
     * @param userPhone 用户的电话号码字符串，用于查询
     * @return 存在返回1，不存在返回0
     *
     * 此方法通过电话号码查询用户表，以确定用户是否已注册
     * 使用LambdaQueryWrapper进行条件查询，提高查询效率和代码可读性
     */
    public int isExist(String userPhone) {
        // 创建LambdaQueryWrapper对象，用于构建查询条件
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件：电话号码等于传入的userPhone
        queryWrapper.eq(UserPO::getPhone,userPhone);
        // 调用userMapper的selectCount方法执行查询并获取结果，转换为int类型返回
        return userMapper.selectCount(queryWrapper).intValue();
    }

    @Override
    public void add(UserPO userPO) {
        userMapper.insert(userPO);
    }

    @Override
    public UserPO detail(Integer id) {
        UserPO userPO = userMapper.selectById(id);
        return userPO;
    }

    @Override
    public UserPO getById(Integer userId) {
        UserPO userPO = userMapper.selectById(userId);
        return userPO;
    }

    @Override
    public List<UserPO> getPasswordByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        List<UserPO> list  =  userMapper.selectList(queryWrapper);
        return list;

    }

    @Override
    public PageInfo<UserPO> showList(UserQuery userQuery) {
        log.info("仓储层用户列表入参{}",userQuery);
        List<UserPO> userPOS = userMapper.selectList(null);
        PageHelper.startPage(userQuery.getPageNo(), userQuery.getPageSize());
        return new PageInfo<>(userPOS);
    }


}
